const kafka = require('kafka-node')

const KeyedMessage = kafka.KeyedMessage
const client = new kafka.KafkaClient({kafkaHost: '127.0.0.1:9092'})
const km = new KeyedMessage('key', 'message')

const Producer = kafka.Producer
const Consumer = kafka.Consumer

const producer = new Producer(client)
const consumer =  new Consumer(client)

const payloads = [
	{ topic: 'topic1', messages: 'hi', partition: 0 },
  { topic: 'topic2', messages: ['hello', 'world', km] }
]

producer.on('ready', () => {
	setInterval(() => {
		producer.send(payloads, (err, data) => {
			if(err) {
				console.log('send data error', err)
			}

			console.log('producer send data', data)
		})
	}, 1500)
})

consumer.on('message', (msg) => {
	console.log('consume message from kafka', msg)
})

producer.on('error', (err) => {
	console.log('producer error', err)
})